package com.yinhai.redis.cluster;

import redis.clients.jedis.JedisPool;

import com.yinhai.redis.cluster.model.KeyRange;
import com.yinhai.redis.cluster.model.ShardInfo;

public class KeyMatchers {
	
	
	private ShardInfo info;
	
	private JedisPool pool;
	
	/**
	 * 备份当前节点的节点连接池(一般为上一节点)
	 */
	private JedisPool masterPool;
	

	public ShardInfo getInfo() {
		return info;
	}

	public void setInfo(ShardInfo info) {
		this.info = info;
	}

	public JedisPool getPool() {
		return pool;
	}

	public void setPool(JedisPool pool) {
		this.pool = pool;
	}
	

	public JedisPool getMasterPool() {
		return masterPool;
	}

	public void setMasterPool(JedisPool masterPool) {
		this.masterPool = masterPool;
	}

	public boolean matchs(int hash){
		if(info.getMasterRange()==null) return false;
		for(KeyRange range:info.getMasterRange()){
			if(hash>range.getStart() && hash<range.getEnd()){
				return true;
			}
		}
		if(info.isEnableSlave()){
			for(KeyRange range:info.getSlave().getMasterRange()){
				if(hash>range.getStart() && hash<range.getEnd()){
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean slaveMatchs(int hash){
		if(info.getSlave().getMasterRange()==null) return false;
		for(KeyRange range:info.getSlave().getMasterRange()){
			if(hash>range.getStart() && hash<range.getEnd()){
				return true;
			}
		}
		return false;
	}
	
	

}
